package niumao.minecraft.uhc.indicator;

import net.md_5.bungee.api.ChatColor;
import niumao.minecraft.uhc.Main;
import org.bukkit.GameMode;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.text.DecimalFormat;


public class ArrowShot implements Listener {
    @EventHandler
    public void on(EntityDamageByEntityEvent e) {
        if (e.getEntity() instanceof Player) {
            Player damaged = (Player) e.getEntity();
            if (!(e.getDamager() instanceof Player) && e.getDamager() instanceof Arrow && ((Projectile) e.getDamager()).getShooter() instanceof Player) {
                Player damager = (Player) ((Projectile) e.getDamager()).getShooter();
                if (damager == null) return;
                double currentHealth = damaged.getHealth();
                if (damaged.getGameMode() == GameMode.SPECTATOR) currentHealth = 0;
                if (Main.getLang().equalsIgnoreCase("en_us")) {
                    damager.sendMessage(ChatColor.AQUA + "Hit " + ChatColor.RED + damaged.getDisplayName()
                            + ChatColor.AQUA + " for " + new DecimalFormat("#.#").format(e.getFinalDamage()) + " damage");
                    damager.sendMessage(damaged.getDisplayName() + ChatColor.YELLOW + " is on "
                            + ChatColor.RED + new DecimalFormat("#.#").format(currentHealth) + ChatColor.YELLOW + " HP!");
                    return;
                }
                damager.sendMessage(ChatColor.AQUA + "击中 " + ChatColor.RED + damaged.getDisplayName()
                        + ChatColor.AQUA + " 造成 " + new DecimalFormat("#.#").format(e.getFinalDamage()) + " 点伤害");
                damager.sendMessage(damaged.getDisplayName() + ChatColor.YELLOW + " 目前生命值为 "
                        + ChatColor.RED + new DecimalFormat("#.#").format(currentHealth) + ChatColor.YELLOW + " HP!");
            }
        }
    }
}
